//
//  RootViewController.m
//  MetenEnglish
//
//  Created by Simon on 14/12/4.
//  Copyright (c) 2014年 Shenzhen Securities Information Co., Ltd. All rights reserved.
//

#import "RootViewController.h"
#import "ViewController.h"
#import "ResevedListController.h"
#import "CourseListController.h"
#import "SVProgressHUD.h"



@interface RootViewController () {
    
    ViewController *_viewController;
    UITabBarController *_tabBarController;
    
    ResevedListController *_reservedCtl;
    CourseListController *_coursesCtl;
    SettingViewController *_settingCtl;
}

@end

@implementation RootViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[HttpManager share] setLoginDelegate:self];
    
    BOOL isPersistLogin = [[[NSUserDefaults standardUserDefaults] objectForKey:PERSISTLOGIN_KEY] boolValue];
    
    if (isPersistLogin) {
        NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:USERNAME_KEY];
        NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:PASSWORD_KEY];
        
        [[HttpManager share] loginMetenWithUserName:userName
                                           password:password];
        
        [self initTabBar];
        [self.view addSubview:_tabBarController.view];
    }else {
        _viewController = [[ViewController alloc] init];
        [self addChildViewController:_viewController];
        [self.view addSubview:_viewController.view];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)changeRootView {
    
    [self initTabBar];
    
    [self transitionFromViewController:_viewController
                      toViewController:_tabBarController
                              duration:0
                               options:UIViewAnimationOptionLayoutSubviews
                            animations:^
    {
        
    } completion:^(BOOL finished) {
        
    }];
    
    [self getBasicData];
}


- (void)getBasicData {
    [[HttpManager share] setDelegate:self];
    [[HttpManager share] searchCourses:1];
}


- (void)initTabBar {
    
    _reservedCtl = [[ResevedListController alloc] init];
    _coursesCtl = [[CourseListController alloc] init];
    _settingCtl = [[SettingViewController alloc] init];
    _settingCtl.delegate = self;
    
    UINavigationController *reservedNavigationCtl = [[UINavigationController alloc] initWithRootViewController:_reservedCtl];
    [self setTabbarItemStyle:reservedNavigationCtl
                       title:@"待上课程"
                  imageNamed:@"book-simple-7.png"];
    
    UINavigationController *coursesNavigationCtl = [[UINavigationController alloc] initWithRootViewController:_coursesCtl];
    [self setTabbarItemStyle:coursesNavigationCtl
                       title:@"订课"
                  imageNamed:@"book-cover-7.png"];
    
    UINavigationController *settingNavigationCtl = [[UINavigationController alloc] initWithRootViewController:_settingCtl];
    [self setTabbarItemStyle:settingNavigationCtl
                       title:@"设置"
                  imageNamed:@"spanner-7.png"];
    
    _tabBarController = [[UITabBarController alloc] init];
    _tabBarController.tabBar.tintColor = OVERALL_COLOR;
    [_tabBarController setViewControllers:@[reservedNavigationCtl,coursesNavigationCtl,settingNavigationCtl]];
    
    [self addChildViewController:_tabBarController];
}

- (void)setTabbarItemStyle:(UINavigationController *)nav title:(NSString *)title imageNamed:(NSString *)imageNamed{
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [UIImage imageNamed:imageNamed];
    nav.tabBarItem.selectedImage = [UIImage imageNamed:imageNamed];
}

#pragma mark - LoginDelegate

- (void)loginSuccess {
    [SVProgressHUD dismiss];
    BOOL isPersistLogin = [[[NSUserDefaults standardUserDefaults] objectForKey:PERSISTLOGIN_KEY] boolValue];
    //如果是持续登录
    if (isPersistLogin) {
        //获取订课记录,订课列表
        [self getBasicData];
        NSString *level = [[NSUserDefaults standardUserDefaults] objectForKey:LEVEL_KEY];
        [[HttpManager share] courseList:44 productLevelId:[level intValue]];
    }else {
        //非持续登录,登录完成之后进入主界面
        [self changeRootView];
    }
    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:PERSISTLOGIN_KEY];
    
}

#pragma mark - HttpManagerDelegate

- (void)searchCoursesSuccess:(NSArray *)courses {
    [_reservedCtl showCoursList:courses];
}

- (void)courseListSuccess:(NSArray *)courses {
    [_coursesCtl showCoursList:courses];
}

#pragma mark - UserLevelDelege
/**
 *  非持续登录获取学生级别成功
 *
 *  @param level 学生级别
 */
- (void)getUserLevel:(NSString *)level {
    //将学生级别保存
    [[NSUserDefaults standardUserDefaults] setObject:level forKey:LEVEL_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[HttpManager share] courseList:44 productLevelId:[level intValue]];
}

#pragma mark - loginOut
- (void)loginOut {
    _viewController = [[ViewController alloc] init];
    [self addChildViewController:_viewController];
    
    [self transitionFromViewController:_tabBarController
                      toViewController:_viewController
                              duration:0
                               options:UIViewAnimationOptionLayoutSubviews
                            animations:^
     {
         
     } completion:^(BOOL finished) {
         
     }];
    
    //清理数据和UI
    [_reservedCtl removeFromParentViewController];
    _reservedCtl = nil;
    [_coursesCtl removeFromParentViewController];
    _coursesCtl = nil;
    [_settingCtl removeFromParentViewController];
    _settingCtl = nil;
    [_tabBarController removeFromParentViewController];
    _tabBarController = nil;
    
    [[HttpManager share].reservedCourses removeAllObjects];
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:LEVEL_KEY];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:PERSISTLOGIN_KEY];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
